package com.dk.logc.business;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.logc.domain.GameRole;
import com.dk.logc.kafka.base.BaseRoleFlog;
import com.dk.logc.service.GameRoleService;
import com.dk.logc.utils.ErrorCode;
import com.dk.logc.utils.LogicException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author 林道敬
 * @Date 2024-01-11
 * @Description
 **/
@Component
@Slf4j
public class GameRoleBusiness {
    @Autowired
    private GameRoleService gameRoleService;
    public Boolean getGameRole(BaseRoleFlog baseRoleFlog)  {

        QueryWrapper<GameRole> gameRoleQueryWrapper = new QueryWrapper<>();
        String channelId = baseRoleFlog.getChannelId();
        Integer serverNo = baseRoleFlog.getServerNo();
        Long chrId = baseRoleFlog.getChrId();
        if (StrUtil.isBlank(channelId) || serverNo == null || chrId == null) {
            log.warn("获取角色信息失败 <<[channelId:{},serverNo:{},chrId:{}]");
            throw new LogicException(ErrorCode.ERR_NULL_VAL);
        }
        gameRoleQueryWrapper.eq("channel_id",channelId)
                .eq("server_no",serverNo)
                .eq("chr_id",chrId);
        GameRole gameRole = gameRoleService.getOne(gameRoleQueryWrapper);
        if (gameRole == null) {
            //角色信息不存在,创建角色信息
            GameRole newGameRole = new GameRole();
            newGameRole.setChannelId(channelId);
            newGameRole.setServerNo(serverNo);
            newGameRole.setChrId(chrId);
            newGameRole.setCreateTime(new Date());
            newGameRole.setUpdateTime(new Date());
//            newGameRole.setLastLoginTime(new Date());
            gameRoleService.save(newGameRole);
            return true;
        }
        return false;
    }


    public GameRole getGameRole(BaseRoleFlog baseRoleFlog,String str)  {

        QueryWrapper<GameRole> gameRoleQueryWrapper = new QueryWrapper<>();
        String channelId = baseRoleFlog.getChannelId();
        Integer serverNo = baseRoleFlog.getServerNo();
        Long chrId = baseRoleFlog.getChrId();
        if (StrUtil.isBlank(channelId) || serverNo == null || chrId == null) {
            log.warn("获取角色信息失败 <<[channelId:{},serverNo:{},chrId:{}]");
            throw new LogicException(ErrorCode.ERR_NULL_VAL);
        }
        gameRoleQueryWrapper.eq("channel_id",channelId)
                .eq("server_no",serverNo)
                .eq("chr_id",chrId);
        GameRole gameRole = gameRoleService.getOne(gameRoleQueryWrapper);
        if (gameRole == null) {
            //角色信息不存在,创建角色信息
            GameRole newGameRole = new GameRole();
            newGameRole.setChannelId(channelId);
            newGameRole.setServerNo(serverNo);
            newGameRole.setChrId(chrId);
            newGameRole.setCreateTime(new Date());
            newGameRole.setUpdateTime(new Date());
//            newGameRole.setLastLoginTime(new Date());
            gameRoleService.save(newGameRole);
            return newGameRole;
        }
        return gameRole;
    }

    public boolean saveOrUpdate(GameRole gameRole){
        boolean b = gameRoleService.saveOrUpdate(gameRole);
        return b;
    }
}
